<?php
namespace Act\Os;
use Lib\ErrorMessage;
class Menu extends Backend{
	public function doGet(){

	}

	public function doAdd(){
		if(is_post()){
			$menu_name = $menu_url = $module_id = $is_show = $online = $shortcut_allowed = $menu_desc = $father_menu = '';
			$menu_url_mod = model('#MenuUrl','adm_');
			extract ( $_POST, EXTR_IF_EXISTS );
			if($menu_name=="" || $menu_url == ""|| $module_id == ""){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$menu = $menu_url_mod->getMenuByUrl($menu_url);
				if(!empty($menu)){
					$this->alert("error",ErrorMessage::MENU_URL_CONFLICT);
				}else{
					$input_data = array ('menu_name' => $menu_name, 'menu_url' => $menu_url, 'module_id' => $module_id, 
										'is_show' => $is_show, 'online' =>1 , 'menu_desc' => $menu_desc ,'shortcut_allowed'=>$shortcut_allowed,'father_menu'=>$father_menu );
					$menu_id = $menu_url_mod->addMenu ( $input_data );
					
					if ($menu_id) {
						model('#SysLog','adm_')->addLog (session('user_info')['user_name'], 'ADD', 'MenuUrl' ,$menu_id ,json_encode($input_data));
						return $this->success ('已将链接添加','os/menus');
					}else{
						$this->alert("error");
					}
				}
			}
		}
		$this->display('from.php',array(
			'module_options_list'=>model('#Module','adm_')->getModuleForOptions(),
			'father_menu_options_list' => model('#MenuUrl','adm_')->getFatherMenuForOptions()
		));
	}

	public function doUpdate(){
		$menu_name = $menu_url = $module_id = $is_show =$online = $shortcut_allowed = $menu_desc = $father_menu = '';
		$menu_id = get('menu_id',0,'absint');
		$this->checkParam($menu_id);
		$menu_url_mod = model('#MenuUrl','adm_');

		$menu = $menu_url_mod->getMenuById ( $menu_id );
		if(empty($menu)){
			return $this->error(ErrorMessage::MENU_NOT_EXIST,"os/menus");
		}

		if(is_post()){
			extract ( $_POST, EXTR_IF_EXISTS );
			if($menu_name == "" || $menu_url =="" || ($menu_id>100 && empty($module_id)) ){
				$this->alert("error",ErrorMessage::NEED_PARAM);
			}else{
				$exist = false;
				$menu_exist = $menu_url_mod->getMenuByUrl($menu_url);
				if(!empty($menu_exist)){
					if($menu_id!=$menu_exist['menu_id']){
						$exist=true;
						$this->alert("error",ErrorMessage::MENU_URL_CONFLICT);
					}
				}
				if(!$exist){
					$update_data = array ('menu_name' => $menu_name, 'menu_url' => $menu_url,  
										'is_show' => $is_show, "online" => $online,'menu_desc' => $menu_desc, 'shortcut_allowed' => $shortcut_allowed,
										'father_menu' => $father_menu);
					if($menu_id > 100){
						$update_data['module_id'] = $module_id;
					}
					
					$result = $menu_url_mod->updateMenuInfo ( $menu_id,$update_data );
					
					if ($result>=0) {
						model('#SysLog','adm_')->addLog ( session('user_info')['user_name'], 'MODIFY', 'MenuUrl' ,$menu_id, json_encode($update_data) );
						return $this->success ('更新完成','os/menus');
					} else {
						$this->error("error",'os/menus');
					}
				}
			}
		}

		$this->display('update.php',array(
			'menu' => $menu,
			'module_options_list' => model('#Module','adm_')->getModuleForOptions (),
			'father_menu_options_list' => $menu_url_mod->getFatherMenuForOptions (),
			'show_options_list' => array("1"=>"显示","0"=>"不显示"),
			'online_options_list' => array("1"=>"在线","0"=>"下线"),
			'shortcut_allowed_options_list' => array("1"=>"允许","0"=>"不允许"),
		));
	}
}